package com.oocl.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.oocl.dao.FoodsDao;
import com.oocl.dao.MerchantsDao;
import com.oocl.dao.OrderCommentDao;
import com.oocl.dao.OrdersDao;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.entity.OrderComment;
import com.oocl.entity.Orders;
import com.oocl.service.FirstPageService;
@Service(value="firstPageService")
public class FirstPageServiceImpl implements FirstPageService {
	@Resource(name="merchantsDao")
	private MerchantsDao merchantsDao;
	@Resource(name="foodsDao")
	private FoodsDao foodsDao;
	@Resource(name="orderCommentDao")
	private OrderCommentDao orderCommentDao;
	@Override
	@Transactional
	public List<Merchants> showMerchants() {
		List<Merchants> merchants = merchantsDao.findAliveMerchants();
		System.out.println(merchants.size());
		return merchants;
	}
	@Override
	@Transactional
	public List<Foods> showFoodsByMerchant(String icnum) {
		List<Foods> foodsList = foodsDao.findFoodsByIcnum(icnum);
		return foodsList;
	}
	@Override
	@Transactional
	public Merchants findmerchantsByIcnum(String icnum) {
		Merchants merchant = merchantsDao.findmerchantsByIcnum(icnum);
		return merchant;
	}
	@Override
	public List<Merchants> findmerchantsByMname(String mname) {
		List<Merchants> merchantList = merchantsDao.findmerchantsByMname(mname);
		return merchantList;
	}
	@Override
	public String computeRatingsLevelWithIcnum(String icnum) {
		List<OrderComment> commondList = orderCommentDao.findCommentListByIcnum(icnum);
		if(commondList.size()==0){
			return "尚未有用户评价";
		}else{
			double total = 0;
			for(OrderComment commend:commondList){
				total += commend.getOclevel();
			}
			double avg = (total/commondList.size());
			DecimalFormat df = new DecimalFormat("#.00");
			String dbNum = df.format(avg);
			return dbNum;
		}
		
		
	}

}
